package com.zhao.service.impl;

import com.zhao.model.Fruit;
import com.zhao.service.PromotionStrategy;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.Map;


// 折扣促销
public class DiscountPromotion implements PromotionStrategy {

    private Map<String, BigDecimal> discounts;

    public DiscountPromotion(Map<String, BigDecimal> discounts) {
        this.discounts = discounts;
    }

    @Override
    public BigDecimal calculate(Map<Fruit, Integer> fruitQuantities) {
        BigDecimal totalPrice = BigDecimal.ZERO;
        for (Map.Entry<Fruit, Integer> entry : fruitQuantities.entrySet()) {
            Fruit fruit = entry.getKey();
            int quantity = entry.getValue();
            BigDecimal price = fruit.getPrice().multiply(BigDecimal.valueOf(quantity));
            BigDecimal discountRate = discounts.getOrDefault(fruit.getName(), BigDecimal.ONE);
            price = price.multiply(discountRate);
            totalPrice = totalPrice.add(price);
        }
        return totalPrice.setScale(2, RoundingMode.HALF_UP);
    }

    @Override
    public int getPriority() {
        return 2; // 多种水果打折策略优先级中等
    }
}
